package com.platform.service.impl;

import com.platform.common.util.DateUtil;
import com.platform.common.util.StringUtil;
import com.platform.dao.RoleAuthorizationDao;
import com.platform.dao.RoleDao;
import com.platform.domain.model.Role;
import com.platform.domain.model.RoleAuthorization;
import com.platform.domain.result.TreeData;
import com.platform.service.RoleService;
import com.platform.service.common.Constants;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * Role
 * <p/>
 * User: wangyang
 * DateTime: 2012-10-08 16:11:58
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleDao roleDao;

    @Resource
    private RoleAuthorizationDao roleAuthorizationDao;

    /**
     * 根据用户ID查询符合条件的角色集合
     *
     * @param userId 用户ID
     * @return 角色集合
     * @throws Exception
     */
    @Override
    public List<Role> selectByUserId(String userId) throws Exception {
        return roleDao.selectByUserId(userId);
    }

    /**
     * 根据用户ID查询除用户授权角色以外的角色集合
     *
     * @param userId 用户ID
     * @return 角色集合
     * @throws Exception
     */
    @Override
    public List<Role> selectByNotUserId(String userId) throws Exception {
        return roleDao.selectByNotUserId(userId);
    }

    /**
     * 根据主键查询Role对象
     *
     * @param roleId
     * @return Role对象
     */
    @Override
    public Role selectByPrimaryKey(String roleId) throws Exception {
        return roleDao.selectByPrimaryKey(roleId);
    }

    /**
     * 根据查询条件查询符合条件的Role对象
     *
     * @param role
     * @return 符合条件的Role对象List
     */
    @Override
    public List<Role> select(Role role) throws Exception {
        return roleDao.select(role);
    }

    /**
     * 根据主键删除Role对象
     *
     * @param roleId
     * @return 影响条件数
     */
    @Override
    public int deleteByPrimaryKey(String roleId) throws Exception {
        return roleDao.deleteByPrimaryKey(roleId);
    }

    /**
     * 根据条件删除符合条件的Role对象
     *
     * @param role
     * @return 影响条件数
     */
    @Override
    public int delete(Role role) throws Exception {
        role.setUpdateTime(DateUtil.toDayString(DateUtil.YYYY_MM_DD_24H_MM_SS));
        role.setDeltag(Constants.IS_TRUE);
        return roleDao.update(role);
    }

    /**
     * 插入Role对象
     *
     * @param role
     * @return 影响条件数
     */
    @Override
    public int insert(Role role) throws Exception {
        role.setRoleId(StringUtil.getUUID());
        role.setCreateTime(DateUtil.toDayString(DateUtil.YYYY_MM_DD_24H_MM_SS));
        role.setDeltag(Constants.IS_FALSE);
        return roleDao.insert(role);
    }

    /**
     * 更新Role对象
     *
     * @param role
     * @return 影响条件数
     */
    @Override
    public int update(Role role) throws Exception {
        role.setUpdateTime(DateUtil.toDayString(DateUtil.YYYY_MM_DD_24H_MM_SS));
        return roleDao.update(role);
    }

    /**
     * 插入Role授权信息
     *
     * @param role
     * @return 影响条件数
     */
    @Override
    public int insertRoleFunctions(Role role) throws Exception {
        String roleId = role.getRoleId();
        String functionStr = role.getRoleName();
        String stateStr = role.getOrderId();
        String[] functions = functionStr.split(Constants.SPLIT_MARK);
        String[] states = stateStr.split(Constants.SPLIT_MARK);

        // 删除原有授权
        RoleAuthorization auth = new RoleAuthorization();
        auth.setRoleId(roleId);
        int resultInt = roleAuthorizationDao.delete(auth);

        // 插入新的授权
        for(int i = 0; i < functions.length; i++) {
            String fun = functions[i];
            if (StringUtil.isBlank(fun) || fun.equals("-1")) {
                continue;
            }
            String state = states[i];
            auth = new RoleAuthorization();
            auth.setRoleId(roleId);
            auth.setFunctionId(fun);
            auth.setCreateTime(DateUtil.toDayString(DateUtil.YYYY_MM_DD_24H_MM_SS));
            auth.setDeltag(Constants.IS_FALSE);
            resultInt += roleAuthorizationDao.insert(auth);
            /*
            if (state.equals(TreeData.STATE_CLOSED)) {
                resultInt += roleAuthorizationDao.insertByFunSp(auth);
            }*/
        }

        return resultInt;
    }
}
